package vn.student;

import java.util.ArrayList;
import vn.student.controller.ViewClassDetailController;
import vn.student.mylib.Assignment;
import vn.student.mylib.Constants;
import vn.student.mylib.ListAssignmentAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import android.widget.RelativeLayout;

	/*
	
		Class name: ViewClassDetail.java
		Function: Day la lop ke thua lop Activity, la lop khoi tao cua so Activity dung de xem thong tin chi tiet mot lop mon hoc (Class).
		
	*/

public class ViewClassDetail extends Activity implements View.OnClickListener{
	boolean isEdit = false;
	String id=null;
	ViewClassDetailController controller = null;
	Activity act = this;
	boolean isEditSomething = false;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.class_detail);
		controller = new ViewClassDetailController(this);
		// Gan doi tuong Listener de su ly su kien nhan nut tren man hinh chinh.
		RelativeLayout tmp = (RelativeLayout) findViewById(R.id.button01);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickBackButton();
					
			}
		});
		tmp = (RelativeLayout) findViewById(R.id.button02);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onClickEditButton();
			}
		});
		
		tmp = (RelativeLayout) findViewById(R.id.addNewAssignmentBtn);
		tmp.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				addNewAssignment();
			}
		});
		Intent intent = this.getIntent();
		id = intent.getExtras().getString("id");
		Boolean isViewStudentAss = intent.getExtras().getBoolean("isViewStudentAss");
		controller.showClassDetail(id);
	}
	/*
		Method name: private void addNewAssignment().
		Function: Them mot bai tap moi vao lop mon hoc hien tai.
		Parameter: None.
		Return: None.
		Note:
    */
	private void addNewAssignment(){
		isEditSomething = true;
		// Tao doi tuong kieu Intent de truyen du lieu cho Activity sap mo de tao moi 1 Ass.
		Intent intent = new Intent(act,ViewAssignmentDetail.class);
		Bundle bundle = new Bundle();
		bundle.putString("id", "-1");
		bundle.putBoolean("isEdit", true);
		bundle.putBoolean("isCreateNew", true);
		bundle.putString("classId", controller.classInfo.id);
		intent.putExtras(bundle);
		// Start mot Activity cho phep nhap vao thong tin chi tiet cho mot bai tap.
		act.startActivityForResult(intent, Constants.ViewAddNewAssignmentDetailRequestCode);
	}
	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/*
		Method name: public void onClickBackButton().
		Function: Thoat Activity hien tai de tro ve Activity truoc do. Xac dinh ket qua tra ve xem Activity truoc do co
			can load lai du lieu hay khong.
		Parameter: None.
		Return: None.
		Note:
    */
	public void onClickBackButton(){
		// Neu co cap nhap thi load lai du lieu khi tro ve Activity truoc do. Nguoc lai khong can load lai.
		if(isEditSomething)
			this.setResult(RESULT_OK);
		else
			this.setResult(RESULT_CANCELED);
		this.finish();
	}
	/*
		Method name: public void onClickEditButton().
		Function: Mo Activity moi de chinh sua thong tin chi tiet ve lop mon hoc dang xem.
		Parameter: None.
		Return: None.
		Note:
    */
	public void onClickEditButton(){
		isEditSomething = true;
		// Khoi tao doi tuong kieu Intent de truyen du lieu cho Activity sap mo.
		Intent intent = new Intent(this,ViewEditClassDetailEditor.class);
		Bundle bundle = new Bundle();
		// Gan id cua lop mon hoc muon chinh sua thong tin vo Intent de truyen qua Activity sap mo.
		bundle.putString("id", id);
		bundle.putBoolean("isCreateNew", false);
		intent.putExtras(bundle);
		// Mo Activity moi de chinh sua thong tin chi tiet ve 1 lop mon hoc.
		this.startActivityForResult(intent, Constants.ViewEditClassDetailEditorRequestCode);
	}
	/*
		Method name: protected void onActivityResult(int requestCode, int resultCode, Intent data).
		Function: Don nhan su kien cac Activity la con cua Activity hien tai duoc tat di.
		Parameter: None.
		Return: None.
		Note:
    */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// Don nhan su kien Activity con duoc dung de chinh sua thong tin chi tiet mot lop mon hoc duoc tat di.
		if(requestCode==Constants.ViewEditClassDetailEditorRequestCode){
			if(resultCode==RESULT_OK){
				isEditSomething = true;
				controller.showClassDetail(id);
			}
		}
		// Don nhan su kien Activity con duoc dung de chinh sua thong tin chi tiet mot bai tap (Assignment) duoc tat di.
		if(requestCode==Constants.ViewEditAssignmentDetailRequestCode){
			controller.showAssignmentList();
		}
		// Don nhan su kien Activity con duoc dung de them moi mot bai tap duoc tat di.
		if(requestCode==Constants.ViewAddNewAssignmentDetailRequestCode){
			if(resultCode==RESULT_OK){
				isEditSomething = true;
				controller.showAssignmentList();
			}
		}
		// Don nhan su kien Activity con duoc dung de chinh sua thong tin chi tiet mot bai tap (Assignment) duoc tat di.
		if(requestCode==Constants.ViewAssignmentDetailRequestCode){
			if(resultCode==RESULT_OK){
				isEditSomething = true;
				controller.showAssignmentList();
			}
		}
	}
	/*
		Method name: public boolean onContextItemSelected(MenuItem  aItem).
		Function: Don nhan su kien cac Activity la con cua Activity hien tai duoc tat di.
		Parameter: None.
		Return: None.
		Note:
    */
	@Override
    public boolean onContextItemSelected(MenuItem  aItem) {
    	AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) aItem.getMenuInfo();
    	ListView listAssView = (ListView)this.findViewById(R.id.list_ass);
    	Assignment assInfo = ((ListAssignmentAdapter)listAssView.getAdapter()).list.get(menuInfo.position);
    	ArrayList<Assignment> list = ((ListAssignmentAdapter)listAssView.getAdapter()).list; 
         /* Switch on the ID of the item, to get what the user selected. */
         switch (aItem.getItemId()) {
              case Constants.CLASS_DETAIL_CONTEXTMENU_ADD_ASS: 
            	  addNewAssignment();
                  return true;
              case Constants.CLASS_DETAIL_CONTEXTMENU_EDIT_ASS:
            	  Intent intent = new Intent(act,ViewAssignmentDetail.class);
            	  Bundle bundle = new Bundle();
            	  bundle.putString("id", assInfo.id);
            	  bundle.putBoolean("isEdit", true);
            	  bundle.putBoolean("isCreateNew", false);
            	  bundle.putString("classId", "-1");
            	  intent.putExtras(bundle);
            	  act.startActivityForResult(intent, Constants.ViewEditAssignmentDetailRequestCode);
            	  return true;
              case Constants.CLASS_DETAIL_CONTEXTMENU_DELETE_ASS:
            	  controller.removeAssignment(assInfo.id);
            	  controller.showAssignmentList();
            	  isEditSomething = true;
            	  return true;
         }
         return false;
    }
}
